//
//  ViewGeneratorProtocol.swift
//  TTCOM
//
//  Created by Toj on 6/9/22.
//

import UIKit

public extension UICollectionView {
    
    func setScrollToTop(animated: Bool) {
        setContentOffset(.zero, animated: animated)
    }
    
    func stopScroll() {
        setContentOffset(contentOffset, animated: false)
    }
}

public extension UICollectionView {
    
    func register<T: UICollectionViewCell>(cellClass T: T.Type) {
        register(T.cell_class, forCellWithReuseIdentifier: T.cell_identifier)
    }
    
    func dequeueReusableCell<T: UICollectionViewCell>(withReuseIdentifierClass T: T.Type, for indexPath: IndexPath) -> T {
        
        var cell = dequeueReusableCell(withReuseIdentifier: T.cell_identifier, for: indexPath) as? T
        if cell == nil { cell = T.init() }
        return cell!
    }
    
    func cellForItem<T: UICollectionViewCell>(at indexPath: IndexPath, convertTo T: T.Type) -> T? {
        let cell = cellForItem(at: indexPath) as? T
        return cell
    }
}

public extension UICollectionView {
    
    func register<T: UICollectionReusableView>(viewClass T: T.Type, forSupplementaryViewOfKind elementKind: String) {
        register(T.cell_class, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: T.cell_identifier)
    }
    
    func dequeueReusableSupplementaryView<T: UICollectionReusableView>(ofKind elementKind: String, withReuseIdentifierClass T: T.Type, for indexPath: IndexPath) -> T {
        
        var view = dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: T.cell_identifier, for: indexPath) as? T
        if view == nil {
            view = T.init()
        }
        return view!
    }
    
    func supplementaryView<T: UICollectionReusableView>(forElementKind elementKind: String, at indexPath: IndexPath, convertTo T: T.Type) -> T? {
        let view = supplementaryView(forElementKind: T.cell_identifier, at: indexPath) as? T
        return view
    }
}
